<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit='1'
      action="#"
      :on-preview="preview"
      :class="{disabled: fileComputed}"
      :on-remove='handleRemove'
      :before-upload="beforeUpload"
      :http-request="upload"
      :on-change="changeFile"
      :file-list="fileList"
      :fileList1="fileList"
      ref="tesd"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-progress
      :percentage="percentage"
      style="width:180px"
      v-if="showPercent"
    ></el-progress>
    <el-dialog
      title="图片"
      :visible.sync="showDialog"
    >
      <img
        :src="imgUrl"
        style="width:100%"
        alt=""
      >
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDJYbR4TdYFiwwjHbgW7TaS5TdYFiwvhEW',
  SecretKey: '7zby6yf3D8pHTQ8bpoZ5MNAl7E7lAKFv'
})
export default {
  data () {
    return {
      percentage: 0,
      showPercent: false,
      fileList: [],
      showDialog: false,
      imgUrl: '',
      currentFileUid: null
    }
  },
  methods: {
    preview (file) {
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove (file, fileList) {
      // console.log(file)
      console.log(file, 'file')
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)

      // this.fileList = fileList

      // console.log(this.fileList, 'this.fileList ')
    },
    changeFile (file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    beforeUpload (file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG格式')
        return false
      }
      // 检查大小
      const maxSize = 5 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      // 已经确定当前上传的就是当前的file了
      this.currentFileUid = file.uid
      this.showPercent = true
      return true
    },
    upload (params) {
      this.$refs.tesd.clearFiles()
      // 上传文档
      cos.putObject({
        Bucket: 'houtaiguanli-1306295698', // 存储桶
        Region: 'ap-nanjing', // 地域
        Key: params.file.name,
        Body: params.file,
        StorageClass: 'STANDARD',
        onProgress: (params) => {
          this.percent = params.percent * 100
        }
      }, (err, data) => {
        console.log(err || data);
        // data中有一个statusCode === 200证明上传成功
        if (!err && data.statusCode === 200) {
          this.fileList = this.fileList.map(item => {
            if (item.uid === this.currentFileUid) {
              // 将成功的地址赋值给原来的url属性
              return { url: "https://" + data.Location, upload: true }
            }
            return item
          })


          this.fileList = [...this.fileList]
          // this.fileList = [{ url: 'https://houtaiguanli-1306295698.cos.ap-nanjing.myqcloud.com/params.file.name' }]
          console.log(this.fileList, 'this.fileList')
          setTimeout(() => {
            this.showPercent = false // 隐藏进度条
            this.percentage = 0 // 进度归0
          }, 1000)
          this.$forceUpdate()
        }
      })
    }
  },
  computed: {
    fileComputed () {
      return this.fileList.length === 1
    }
  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none;
}
</style>